/* Copyright (c) 2011 Vitria Technology, Inc.  All rights reserved. */
package com.vitria.m3oui.rest.app.mgr;

import java.io.FileInputStream;
import java.io.IOException;

import org.restlet.data.Form;
import org.restlet.representation.Representation;
import org.restlet.resource.ClientResource;
import com.qilin.hammer.util.BaseRestService;
public class AppHotDeployment extends BaseRestService {
	public static String APP_ID = "/sla";
    public static String APP_URL_PREFIX = URL_PREFIX + "/app.mgr/apps";
    
    public static String INSTALL_UID = "AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAASLA";
    public static String UPGRADE_UID = "BBBBBBBB-AAAA-AAAA-AAAA-AAAAAAAAASLA";
    public static String EXPORT_UID =  "CCCCCCCC-AAAA-AAAA-AAAA-AAAAAAAAASLA";

    //used for KPI model
    public static String kpiUrlBase = URL_PREFIX + "/app/sla/user/vtbaadmin/kpi/SLA";
    public static String saveOp = "?op=save";
	public static String deployOp = "?op=deploy";
	public static String startOp = "?op=start&needDeploy=true";
	public static String stopOp = "?op=stop";
	public static String deleteOp = "?op=delete";
	public static String kpi1ContentFile = "D:/work/main/yoda/m3o/ui/devtests/junit/app.memory/resources/kpi1Content.txt";
    
    
    public AppHotDeployment(String name) {
        super(name);
    }
    
    public void testLoop_install_enable_disable_export_upgrade_unInstall() throws Exception {
        String appID = APP_ID;
        for (int i=1; i<=20; i++) {
            //setUp();
            
             //install SLA1.0 app 
            String url = APP_URL_PREFIX + appID + "?op=install" + "&uid=" + INSTALL_UID;
            ClientResource resource = createSecureClientResource(url);
            //resource.addQueryParameter("uid", "sample.zip");
            Representation rep = resource.get();
            String text = rep.getText();
            System.out.println("loop: " + i);
            System.out.println("install... ");
            
            //list all app
            resource = createSecureClientResource(APP_URL_PREFIX);
            rep = resource.get();
            text = rep.getText();
            System.out.println("list... ");
            
            //enable SLA1.0 app
            url = APP_URL_PREFIX + appID + "?op=enable" ;
            resource = createSecureClientResource(url);
            rep = resource.post(null);
            text = rep.getText();
            System.out.println("enable... ");
            
            //kpi operations
            operationKPIModel(resource,rep,text,"1");
            
        	//disable SLA1.0 app
        	url = APP_URL_PREFIX + appID + "?op=disable" ;
            resource = createSecureClientResource(url);
            rep = resource.post(null);
            text = rep.getText();
            System.out.println("disable... ");
            
            //export SLA1.0 app
            url = APP_URL_PREFIX + appID + "?op=export" + "&uid=" + EXPORT_UID ;
            resource = createSecureClientResource(url);
            rep = resource.post(null);
            text = rep.getText();
            System.out.println("export...");
         
            //upgrade SLA2.0 app
            url = APP_URL_PREFIX + appID + "?op=upgrade" + "&uid=" + UPGRADE_UID ;
            resource = createSecureClientResource(url);
            //resource.addQueryParameter("uid", "sample.zip");
            rep = resource.post(null);
            text = rep.getText();
            System.out.println("upgrade... ");
            
            //enable SLA2.0 app
            url = APP_URL_PREFIX + appID + "?op=enable" ;
            resource = createSecureClientResource(url);
            rep = resource.post(null);
            text = rep.getText();
            System.out.println("enable... ");
            
            operationKPIModel(resource,rep,text,"2");
            
            //uninstall SLA2.0 app
            url = APP_URL_PREFIX + appID + "?op=uninstall" + "&keepAppConf=false" + "&keepUserData=false" ;
            resource = createSecureClientResource(url);
            rep = resource.get();
            text = rep.getText();
            System.out.println("uninstall!");
            assertNotNull(text);
            //tearDown();
        }
    }
    
    private void operationKPIModel(ClientResource resource, 
    		   Representation rep, String text, String index) throws IOException{
    	 //KPI model test 
    	index = "1";
    	String opString;
    	String document = readFromFile(kpi1ContentFile);
		String properties = "<properties><property name='tags' value=''/></properties>";
    	
    	Form form = new Form();
        form.add("_RESOURCE_Type", "kpi");
        form.add("document", document);
        form.add("properties", properties);
        form.add("space", "private");
    	
        // create KPI model SLA{$i}
    	opString = kpiUrlBase + index + saveOp;
    	resource = createSecureClientResource(opString);
    	rep = resource.post(form.getWebRepresentation());
    	text = rep.getText();
    	System.out.print("create KPI"+ index +" model... ");
    	
//    	// deploy KPI model
//    	opString = kpiUrlBase + index + deployOp;
//    	resource = createSecureClientResource(opString);
//    	rep = resource.get();
//    	text = rep.getText();
//    	System.out.print("deploy KPI"+ index +" model... ");
//    	
    	// deploy and start KPI model
    	opString = kpiUrlBase + index  + startOp;
    	resource = createSecureClientResource(opString);
    	rep = resource.get();
    	text = rep.getText();
    	System.out.print("deploy and start KPI"+ index +" model... ");
    	
    	// stop KPI model
    	opString = kpiUrlBase + index  + stopOp;
    	resource = createSecureClientResource(opString);
    	rep = resource.get();
    	text = rep.getText();
    	System.out.print("stop KPI"+ index +" model... ");
    	
    	// delete KPI model
    	opString = kpiUrlBase + index  + deleteOp;
    	resource = createSecureClientResource(opString);
    	rep = resource.get();
    	text = rep.getText();
    	System.out.print("delete KPI"+ index +" model... ");
    }
    
    
    private static String readFromFile(String fileName) {
		try {
			StringBuilder sb = new StringBuilder();
			FileInputStream fstream = new FileInputStream(fileName);
			java.io.DataInputStream is = new java.io.DataInputStream(fstream);
			java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(is));
			String strLine;
			while ((strLine = br.readLine()) != null) {
				sb.append(strLine);
			}
			is.close();
			return sb.toString();
		}
		catch (Exception e) {
			System.err.println("Error: " + e.getMessage());
		}
		return null;
	}
}
